package com.Listeners;

import com.SelfHome.Main;
import com.SelfHome.Variable;
import com.Util.Home;
import com.Util.HomeAPI;
import com.Util.MySQL;
import com.Util.Util;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;








public class PlayerMoveListener
  implements Listener
{
	int addExtra = 0;
    public static HashMap<String,Integer> border_redis = new HashMap<String,Integer>();
    
  @EventHandler
  public void onMove(final PlayerMoveEvent event) { (new BukkitRunnable()
      {
        public void run() {
          Player p = event.getPlayer();
          
          if (p.isOp()) {
            return;
          }
          
          if (!Util.CheckIsHome(p.getWorld().getName().replace(Variable.world_prefix, ""))) {
            return;
          }
          
          
          addExtra = 0;
          Home h = HomeAPI.getHome(event.getTo().getWorld().getName());
          int vip_add = 0;
    	  ArrayList<String> players = new ArrayList<String>();
    	  players.add(h.getName());
    	  for(String op:h.getOPs()) {
    		  if(Bukkit.getPlayer(op) != null) {
    			  players.add(op);
    		  }
    	  }
          for(String s:Main.JavaPlugin.getConfig().getStringList("VIPAdd")) {
        	  String[] ss = s.split(",");
        	  for(String p_name : players) {
        		  Player pe = Bukkit.getPlayer(p_name);
        		  if(pe==null) {
        			  continue;
        		  }
        		  if(pe.hasPermission(ss[0])) {
        			  int add = Integer.valueOf(ss[1]);
        			  if(add > vip_add) {
        				  vip_add = add;
        			  }
        		  }
        		  
        	  }
          }
          addExtra = vip_add;
          
          
          if(border_redis.containsKey(h.getName())) {
              if(addExtra < border_redis.get(h.getName())) {
            	  addExtra = border_redis.get(h.getName());
              }else {
        		  border_redis.put(h.getName(), addExtra);
        	  }
          }else {
              border_redis.put(h.getName(), addExtra);
          }
          
          
          
          double set_x = 0.0D;
          double min_x = 0.0D;
          double set_z = 0.0D;
          double min_z = 0.0D;
          if (Variable.bungee) {
            int level = 
              Integer.valueOf(MySQL.getLevel(p.getWorld().getName().replace(Variable.world_prefix, ""))).intValue();
            set_x = p.getWorld().getSpawnLocation().getX()  + addExtra + Main.JavaPlugin.getConfig().getInt("WorldBoard") / 2 + (
              Main.JavaPlugin.getConfig().getInt("UpdateRadius") / 2 * (level-1));
            min_x = p.getWorld().getSpawnLocation().getX() - addExtra - Main.JavaPlugin.getConfig().getInt("WorldBoard") / 2 - (
              Main.JavaPlugin.getConfig().getInt("UpdateRadius") / 2 * (level-1));
            set_z = p.getWorld().getSpawnLocation().getZ() + addExtra + Main.JavaPlugin.getConfig().getInt("WorldBoard") / 2 + (
              Main.JavaPlugin.getConfig().getInt("UpdateRadius") / 2 * (level-1));
            min_z = p.getWorld().getSpawnLocation().getZ() - addExtra - Main.JavaPlugin.getConfig().getInt("WorldBoard") / 2 - (
              Main.JavaPlugin.getConfig().getInt("UpdateRadius") / 2 * (level-1));
          } else {
            
            File f2 = new File(Variable.Tempf, 
                String.valueOf(p.getWorld().getName().replace(Variable.world_prefix, "")) + ".yml");
            YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(f2);
            Location loc = p.getLocation();
            
            set_x = p.getWorld().getSpawnLocation().getX() + addExtra + Main.JavaPlugin.getConfig().getInt("WorldBoard") / 2 + (
              Main.JavaPlugin.getConfig().getInt("UpdateRadius") / 2 * (yamlConfiguration.getInt("Level")-1));
            min_x = p.getWorld().getSpawnLocation().getX() - addExtra - Main.JavaPlugin.getConfig().getInt("WorldBoard") / 2 - (
              Main.JavaPlugin.getConfig().getInt("UpdateRadius") / 2 * (yamlConfiguration.getInt("Level")-1));
            set_z = p.getWorld().getSpawnLocation().getZ() + addExtra + Main.JavaPlugin.getConfig().getInt("WorldBoard") / 2 + (
              Main.JavaPlugin.getConfig().getInt("UpdateRadius") / 2 * (yamlConfiguration.getInt("Level")-1));
            min_z = p.getWorld().getSpawnLocation().getZ() - addExtra - Main.JavaPlugin.getConfig().getInt("WorldBoard") / 2 - (
              Main.JavaPlugin.getConfig().getInt("UpdateRadius") / 2 * (yamlConfiguration.getInt("Level")-1));
          } 
          
          if (set_x < min_x) {
            double temp = set_x;
            set_x = min_x;
            min_x = temp;
          } 
          
          if (set_z < min_z) {
            double temp = set_z;
            set_z = min_z;
            min_z = temp;
          } 
          
          if (p.getLocation().getX()<= min_x || p.getLocation().getX()>= set_x || 
            p.getLocation().getZ()<= min_z || p.getLocation().getZ()>= set_z) {

            
            if (Main.JavaPlugin.getConfig().getBoolean("EnableAdventureMode") && 
              p.getGameMode() != GameMode.ADVENTURE) {
              p.setGameMode(GameMode.ADVENTURE);
              p.sendMessage(Variable.Lang_YML.getString("PlayerMoveOverBorderButAdventure"));
            } 


            
            if (!Main.JavaPlugin.getConfig().getString("BorderCommand").equalsIgnoreCase("") && 
              !Variable.DispathCommand.contains(p.getName())) {
              Variable.DispathCommand.add(p.getName());
            }
            

                    if (Main.JavaPlugin.getConfig().getBoolean("PlayerMoveOverBorderBuff") && 
                      !Variable.AddDebuff.contains(p.getName()))
                      Variable.AddDebuff.add(p.getName()); 
                    if (Main.JavaPlugin.getConfig().getBoolean("PlayerMoveOverBorderHit"))
                      p.setVelocity(new Vector(0, 0, -3)); 




            
            cancel();
            
            return;
          }
          if (p.getLocation().getX() + 15.0D <= min_x || p.getLocation().getX() - 15.0D >= set_x || 
            p.getLocation().getZ() + 15.0D <= min_z || p.getLocation().getZ() - 15.0D >= set_z) {
            
            if (Main.JavaPlugin.getConfig().getBoolean("PlayerMoveOverBorderBuff") && 
              !Variable.AddDebuff.contains(p.getName())) {
              Variable.AddDebuff.add(p.getName());
            }


            
            if (Main.JavaPlugin.getConfig().getBoolean("PlayerMoveOverBorderHit")) {
              p.setVelocity(new Vector(0, 0, -3));
            }


            
            cancel();
            
            return;
          } 
          if (p.getGameMode() == GameMode.ADVENTURE) {
        	  
        	  new BukkitRunnable()
              {
                public void run()
                {
                	  p.setGameMode(GameMode.SURVIVAL);
                }
              }.runTask(Main.JavaPlugin);
        	  
          

          
          }
        }
      }).runTaskAsynchronously((Plugin)Main.JavaPlugin); }
}


